package indi.luckly.service.impl;

import indi.luckly.common.request.LoginRequestParams;
import indi.luckly.common.request.LogoutRequestParams;
import indi.luckly.common.request.RegisterRequestParams;
import indi.luckly.common.response.LoginResponseData;
import indi.luckly.common.response.LogoutResponseData;
import indi.luckly.common.response.RegisterResponseData;
import indi.luckly.common.response.ResponseResult;;
import indi.luckly.entity.UserEntity;
import indi.luckly.common.key.ErrorKey;
import indi.luckly.mapper.IUserMapper;
import indi.luckly.service.ILoginService;
import indi.luckly.utils.CreateUserUtils;
import indi.luckly.utils.SaltUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service("LoginService")
public class LoginServiceImpl implements ILoginService {

    @Autowired
    @Qualifier("UserMapper")
    IUserMapper mUserMapper;

    @Override
    public ResponseResult<RegisterResponseData> register(RegisterRequestParams params) {
        if (!Objects.equals(params.getPassword(), params.getConfirmPassword())) {
            return ResponseResult.fail(ErrorKey.DIFFERENT_PASSWORDS);
        }
        if (!StringUtils.hasLength(params.getPassword())) {
            return ResponseResult.fail(ErrorKey.NO_PASSWORD_SET);
        }

        String salt = SaltUtils.CreateSalt(8);
        Md5Hash md5Hash = new Md5Hash(params.getPassword(), salt, 1024);
        String passwordMd5HexStr = md5Hash.toHex();
        UserEntity user = new UserEntity();
        user.setUserName(params.getUserName());
        user.setPassword(passwordMd5HexStr);
        user.setSalt(salt);
        String userNumber = CreateUserUtils.CreateUserNumber(10);
        user.setUserNumber(userNumber);

        try {

            mUserMapper.insert(user);
            RegisterResponseData registerResponseData = new RegisterResponseData();
            registerResponseData.setUserNumber(userNumber);
            return ResponseResult.ok(registerResponseData);
        } catch (Exception e) {
            System.out.println(e.toString());
            return ResponseResult.fail(ErrorKey.ACCOUNT_CREATE_FAILURE);
        }
    }

    @Override
    public ResponseResult<LoginResponseData> login(LoginRequestParams params) {
        Subject subject = SecurityUtils.getSubject();
        LoginResponseData loginResponseData = new LoginResponseData();

        UsernamePasswordToken token = new UsernamePasswordToken(params.getUserName(), params.getPassword());
        try {
            subject.login(token);
            loginResponseData.setToken(token.toString());
            return ResponseResult.ok(loginResponseData);
        } catch (UnknownAccountException | IncorrectCredentialsException e) {
            return ResponseResult.fail(ErrorKey.USER_NAME_OR_PASSWORD_NOT_EXISTS, loginResponseData);
        } catch (Exception e) {
            return ResponseResult.fail(ErrorKey.UNKNOWN, loginResponseData);
        }
    }

    @Override
    public ResponseResult<LogoutResponseData> logout(LogoutRequestParams logoutRequestParams) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return null;
    }

    @Override
    public ResponseResult<UserEntity> queryUserInfo(String userId) {
        mUserMapper.selectById(userId);

        return null;
    }
}
